﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lipcas.Common.Extensions;

namespace Douban.Api.Templates
{
    public class Template
    {
        private readonly StringBuilder _templateResourceBuilder;

        public Template(string templateResource)
        {
            _templateResourceBuilder = new StringBuilder(templateResource);
        }

        public Template Replace(string placeholder, string value)
        {
            _templateResourceBuilder.Replace("{" + placeholder + "}", value);
            return this;
        }

        public Template BatchReplace(IEnumerable<Template> templates)
        {
            StringBuilder batchBuilder = new StringBuilder();
            templates.ForEach(t => batchBuilder.Append(t.ToString()));

            _templateResourceBuilder.Replace("entries", batchBuilder.ToString());

            return this;
        }

        public override string ToString()
        {
            return _templateResourceBuilder.ToString();
        }
    }
}
